package com.trp.view;

import android.graphics.Matrix;
import android.graphics.RectF;

public final class Transformation {

	private Matrix mWorldToScreen;
	private Matrix mScreenToWorld;

	private float mScale = 1;

	private static Transformation mInstance;



	private Transformation() {
		mWorldToScreen = new Matrix();

		mScreenToWorld = new Matrix();
	}


	public static synchronized Transformation getInstance() {
		if (mInstance == null) {
			mInstance = new Transformation();
		}
		return mInstance;
	}


	public void setMap(RectF world, RectF screen) {
		//TODO must keep screen's with/screen's height == world's with/world's height
		mWorldToScreen.setRectToRect(world, screen, Matrix.ScaleToFit.CENTER);

		mScreenToWorld.setRectToRect(screen, world, Matrix.ScaleToFit.CENTER);

	}


	public void translate(int x, int y) {
		mWorldToScreen.postTranslate(x, y);
		mWorldToScreen.invert(mScreenToWorld);
	}

	public void scale(float scale, int pointX, int pointY) {
		mScale *= scale;
		mWorldToScreen.postScale(scale, scale, pointX, pointY);
		mWorldToScreen.invert(mScreenToWorld);
	}

	public void mapWorldPointToScreen(float[] points) {
		mWorldToScreen.mapPoints(points);
	}

	public void mapScreenPointToWorld(float[] points) {
		mScreenToWorld.mapPoints(points);
	}


	public float getScale() {
		return mScale;
	}


	public void reset() {
	    mWorldToScreen.reset();
	    mScreenToWorld.reset();
	}
}
